﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject.Code.DAO
{
    public class JobTitleLinkDAO : BaseDAO
    {
        public JobTitleLinkDAO()
            : base ()
        {
        }

        public List<BLJobTitleLink> Search(BLJobTitleLink filter, string sortColumn, bool sortDirection, int totalPage, int pageSize, int pageIndex)
        {
            int startIndex = (pageIndex - 1) * pageSize + 1;
            if (pageIndex > totalPage)
            {
                startIndex = (totalPage - 1) * pageSize + 1;
            }
            else if (pageIndex < 1)
            {
                startIndex = 1;
            }
            int endIndex = startIndex + pageSize - 1;

            string sort = sortColumn + (sortDirection ? "Asc" : "Desc");

            List<BLJobTitleLink> result = new List<BLJobTitleLink>();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@id", filter.Agent.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@name", filter.Agent.FullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@jobTitleId", filter.JobTitle.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@jobStartDownDate", filter.StartDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@jobStartUpDate", filter.EndDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@sort", sort, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startIndex", startIndex, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endIndex", endIndex, ParameterDirection.Input));
            DataTable data = select("usp_SearchAgentsInJobTitle", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLJobTitleLink jobTitleLink = new BLJobTitleLink();
                jobTitleLink.Id = (Guid)data.Rows[i]["JobTitleLinkId"];
                jobTitleLink.StartDate = (DateTime)data.Rows[i]["StartDate"];
                jobTitleLink.EndDate = (DateTime)data.Rows[i]["EndDate"];
                jobTitleLink.Agent = new BLAgent();
                jobTitleLink.Agent.Id = (Guid)data.Rows[i]["AgentId"];
                jobTitleLink.Agent.FullName = (string)data.Rows[i]["FullName"];
                jobTitleLink.Agent.BirthDate = (DateTime)data.Rows[i]["BirthDate"];
                jobTitleLink.Agent.IdentifiedSerial = (string)data.Rows[i]["IdentifiedSerial"];
                jobTitleLink.JobTitle = new BLJobTitle();
                jobTitleLink.JobTitle.Name = (string)data.Rows[i]["JobTitleName"];
                result.Add(jobTitleLink);
            }
            return result;
        }

        public int Add(BLJobTitleLink jobTitleLink, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", jobTitleLink.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentId", jobTitleLink.Agent.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@jobTitleId", jobTitleLink.JobTitle.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startDate", jobTitleLink.StartDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endDate", jobTitleLink.EndDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", jobTitleLink.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_AddJobTitleLink", condition);
            errorCode = (int)condition[condition.Count-1].ParamValue;
            return result;
        }

        public int Delete(BLJobTitleLink jobTitleLink, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", jobTitleLink.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", jobTitleLink.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_DeleteJobTitleLink", condition);
            errorCode = (int)condition[condition.Count-1].ParamValue;
            return result;
        }

        public int Update(BLJobTitleLink jobTitleLink, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", jobTitleLink.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startDate", jobTitleLink.StartDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endDate", jobTitleLink.EndDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", jobTitleLink.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_UpdateJobTitleLink", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }
    }
}
